#include "serialportpro.h"
#include <QDebug>

Serialportpro::Serialportpro ( QObject* parent)
{
    m_serial = new QSerialPort(parent);    
    m_serial->setPortName("/dev/ttymxc1");
    bool ret = m_serial->open(QIODevice::ReadWrite);
    m_serial->setBaudRate(57600);
    m_serial->setDataBits(QSerialPort::Data8);
    m_serial->setParity(QSerialPort::NoParity);
    m_serial->setStopBits(QSerialPort::OneStop);
    m_serial->setFlowControl(QSerialPort::NoFlowControl);


   if(ret)
    {
        m_timer = new QTimer;
        QObject::connect( m_timer, SIGNAL( timeout() ), this, SLOT( comunicate() ) );
        m_timer->start( 10 );
    }

    test_dir = 1;
    test_flr = 1;
    QTimer * t_timer =  new QTimer;
    QObject::connect( t_timer, SIGNAL( timeout() ), this, SLOT( test() ) );
    //t_timer->start( 2500 );

    m_isPrograming = false;
    txrepeat = false;
}

void Serialportpro:: comunicate()
{
    QByteArray rxtmp = m_serial->readAll();
    if(rxtmp.size()>0)
    {
        //qDebug()<<"hehe: "<<rxtmp.toHex();
        m_rxdata += rxtmp;
    }

    demux();    

}

void Serialportpro:: demux()
{
    char asc[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D','E', 'F'};
    int i=1;

    while ( m_rxdata.size() > 3 )
    {
        

        if(m_rxdata[0]!='\x02')
        {
              m_rxdata.remove( 0, 1 );
        }
        else
        {
            if(m_rxdata[1] == 'S')
            {
                if(m_rxdata.size() < 11+16+3)
                    return;
                if(m_rxdata[10+16+3] == '\x03')
                {
                    int Sum = 0;
                    for(i = 1;i<9+16+3;i++)
                        Sum ^= m_rxdata[i];
                    if(asc[Sum&0x0f]==m_rxdata[i])
                    {
                        //qDebug()<<"SS";
                        QByteArray curflr = m_rxdata.mid(2,2);
                        bool ok;
                        int flr = curflr.toInt(&ok,10);
                            emit flr_s(flr);

                        QByteArray curdir = m_rxdata.mid(4,1);
                        int dir = curdir.toInt(&ok,16);

                        if(dir&0b0001)
                        {
                            running = true;
                            emit dir_s(0);
                        }
                        else if(dir&0b0010)
                        {
                            running = true;
                            emit dir_s(1);
                        }
                        else
                        {
                            if(running)
                                emit dir_s(2);
                            running = false;
                        }

                        QByteArray curstate = m_rxdata.mid(5,1);
                         int ste = curstate.toInt(&ok,16);
                            emit state_s(ste);

                        //for(i=0;i<16;i++)
                        for(i=0;i<2;i++)
                        {
                            QByteArray regfstate = m_rxdata.mid(9+i,1);
                            int regf = regfstate.toInt(&ok,16);
                            for(int j=0;j<4;j++)
                            {

                                if(regf&(1<<j))
                                {
                                    emit regflr_s(i*4+j+1);
                                }
                                else
                                {
                                    emit unregflr_s(i*4+j+1);
                                }
                            }
                        }

                        QByteArray lfj_ste = m_rxdata.mid(25,3);
                        if(lfj_ste[1]!=0x0)
                            emit light_s(true);
                        else
                            emit light_s(false);

                        if(lfj_ste[0]!=0x0)
                            emit fan_s(true);
                        else
                            emit fan_s(false);

                        if(lfj_ste[2]!=0x0)
                            emit jl_s(true);
                        else
                            emit jl_s(false);

                        m_rxdata.remove( 0, 11+16+3 );

                    }
                    else
                       m_rxdata.remove( 0, 1 );
                }
                else
                    m_rxdata.remove( 0, 1 );

            }

            else if(m_rxdata[1] == 'C')
            {
                if(m_rxdata.size() < 5)
                    return;
                if(m_rxdata[4] == '\x03')
                {
                    int Sum = 0;
                    for(i = 1;i<3;i++)
                    Sum ^= m_rxdata[i];
                    if(asc[Sum&0x0f]==m_rxdata[i])
                    {
                        qDebug()<<"CC";
                        txrepeat = false;
                        m_rxdata.remove( 0, 5 );

                    }
                    else
                       m_rxdata.remove( 0, 1 );
                }
                else
                    m_rxdata.remove( 0, 1 );
            }

            else if(m_rxdata[1] == 'V')
            {
                if(m_rxdata.size() < 7)
                    return;
                if(m_rxdata[6] == '\x03')
                {
                    int Sum = 0;
                    for(i = 1;i<5;i++)
                    Sum ^= m_rxdata[i];
                    if(asc[Sum&0x0f]==m_rxdata[i])
                    {
                        //qDebug()<<"VV";
                        m_txbuffer.clear();
                        m_txbuffer = m_rxdata.mid(0,7);
                        m_serial->write(m_txbuffer);

                        QByteArray regflr = m_rxdata.mid(2,2);
                        bool ok;
                        int flr = regflr.toInt(&ok,10);
                        if(m_rxdata[4]=='0')
                        {
                                voice_s(flr);
                        }
                        else if(m_rxdata[4]=='1')//open
                        {
                                voice_s(92);
                        }
                        else if(m_rxdata[4]=='2')//close
                        {
                                voice_s(93);
                        }
                        else if(m_rxdata[4]=='3')//up
                        {
                                voice_s(90);
                        }
                        else if(m_rxdata[4]=='4')//down
                        {
                                voice_s(91);
                        }
                        else
                        {
                            ;
                        }

                        m_rxdata.remove( 0, 7 );

                    }
                    else
                        m_rxdata.remove(0,1);
                }
                else
                    m_rxdata.remove(0,1);

            }

            else if(m_rxdata[1] == 'F')
            {
                if(m_rxdata.size() < 6)
                    return;
                if(m_rxdata[5] == '\x03')
                {
                    int Sum = 0;
                    for(i = 1;i<4;i++)
                    Sum ^= m_rxdata[i];
                    if(asc[Sum&0x0f]==m_rxdata[i])
                    {
                        qDebug()<<"FF"<<m_rxdata.toHex();
                        txrepeat = false;
                        m_rxdata.remove( 0, 6 );

                    }
                    else
                       m_rxdata.remove( 0, 1 );
                }
                else
                    m_rxdata.remove( 0, 1 );
            }

            else if(m_rxdata[1] == 'G')
            {
                if(m_rxdata.size() < 6)
                    return;
                if(m_rxdata[5] == '\x03')
                {
                    int Sum = 0;
                    for(i = 1;i<4;i++)
                    Sum ^= m_rxdata[i];
                    if(asc[Sum&0x0f]==m_rxdata[i])
                    {
                        m_txbuffer.clear();
                        m_txbuffer = m_rxdata.mid(0,6);
                        m_serial->write(m_txbuffer);

                        qDebug()<<"GG"<<m_txbuffer.toHex();

                        QByteArray regflr = m_rxdata.mid(2,2);
                        bool ok;
                        int flr = regflr.toInt(&ok,10);
                        //emit regflr_s(flr);

                        m_rxdata.remove( 0, 6 );

                    }
                    else
                       m_rxdata.remove( 0, 1 );
                }
                else
                    m_rxdata.remove( 0, 1 );
            }

            else if(m_rxdata[1] == 'W')
            {
                if(m_rxdata.size() < 20)
                    return;
                if(m_rxdata[19] == '\x03')
                {
                    int Sum = 0;
                    for(i = 1;i<18;i++)
                    Sum ^= m_rxdata[i];
                    if(asc[Sum&0x0f]==m_rxdata[i])
                    {
                       // qDebug()<<"WW";
                        txrepeat = false;
                        QByteArray vis = m_rxdata.mid(2,16);
                        emit vision_s(vis);
                        m_rxdata.remove( 0, 20 );

                    }
                    else
                       m_rxdata.remove( 0, 1 );
                }
                else
                    m_rxdata.remove( 0, 1 );
            }

            else if(m_rxdata[1] == 'O')
            {
                if(m_rxdata.size() < 5)
                    return;
                if(m_rxdata[4] == '\x03')
                {
                    int Sum = 0;
                    for(i = 1;i<3;i++)
                    Sum ^= m_rxdata[i];
                    if(asc[Sum&0x0f]==m_rxdata[i])
                    {
                        qDebug()<<"OO";
                        txrepeat = false;
                        m_rxdata.remove( 0, 5 );

                    }
                    else
                       m_rxdata.remove( 0, 1 );
                }
                else
                    m_rxdata.remove( 0, 1 );
            }

            else if(m_rxdata[1] == 'L')
            {
                if(m_rxdata.size() < 5)
                    return;
                if(m_rxdata[4] == '\x03')
                {
                    int Sum = 0;
                    for(i = 1;i<3;i++)
                    Sum ^= m_rxdata[i];
                    if(asc[Sum&0x0f]==m_rxdata[i])
                    {
                        qDebug()<<"LL";
                        txrepeat = false;
                        m_rxdata.remove( 0, 5 );

                    }
                    else
                       m_rxdata.remove( 0, 1 );
                }
                else
                    m_rxdata.remove( 0, 1 );
            }

            else if(m_rxdata[1] == 'B')
            {
                if(m_rxdata.size() < 5)
                    return;
                if(m_rxdata[4] == '\x03')
                {
                    int Sum = 0;
                    for(i = 1;i<3;i++)
                    Sum ^= m_rxdata[i];
                    if(asc[Sum&0x0f]==m_rxdata[i])
                    {
                        qDebug()<<"BB";
                        txrepeat = false;
                        m_rxdata.remove( 0, 5 );

                    }
                    else
                       m_rxdata.remove( 0, 1 );
                }
                else
                    m_rxdata.remove( 0, 1 );
            }

            else if(m_rxdata[1] == 'E')
            {
                if(m_rxdata.size() < 5)
                    return;
                if(m_rxdata[4] == '\x03')
                {
                    int Sum = 0;
                    for(i = 1;i<3;i++)
                    Sum ^= m_rxdata[i];
                    if(asc[Sum&0x0f]==m_rxdata[i])
                    {
                        qDebug()<<"EE";
                        txrepeat = false;
                        m_rxdata.remove( 0, 5 );

                    }
                    else
                       m_rxdata.remove( 0, 1 );
                }
                else
                    m_rxdata.remove( 0, 1 );
            }

            else if(m_rxdata[1] == 'J')
            {
                if(m_rxdata.size() < 5)
                    return;
                if(m_rxdata[4] == '\x03')
                {
                    int Sum = 0;
                    for(i = 1;i<3;i++)
                    Sum ^= m_rxdata[i];
                    if(asc[Sum&0x0f]==m_rxdata[i])
                    {
                        qDebug()<<"JJ";
                        txrepeat = false;
                        m_rxdata.remove( 0, 5 );

                    }
                    else
                       m_rxdata.remove( 0, 1 );
                }
                else
                    m_rxdata.remove( 0, 1 );
            }

            else if(m_rxdata[1] == 'D')
            {
                if(m_rxdata.size() < 5)
                    return;
                if(m_rxdata[4] == '\x03')
                {
                    int Sum = 0;
                    for(i = 1;i<3;i++)
                    Sum ^= m_rxdata[i];
                    if(asc[Sum&0x0f]==m_rxdata[i])
                    {
                        //qDebug()<<"DD";
                        txrepeat = false;
                        m_rxdata.remove( 0, 5 );

                    }
                    else
                       m_rxdata.remove( 0, 1 );
                }
                else
                    m_rxdata.remove( 0, 1 );
            }

            else
                m_rxdata.remove( 0, 1 );
        }
    }
}

void  Serialportpro::pushCommand ( int cmd )//key pressed
{
    char asc[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D','E', 'F'};
    int i=1;
    if( (cmd>=1)&&(cmd<=8) )
    {
        QByteArray newprotocol1;
        newprotocol1.resize ( 6 );
        newprotocol1[0] = 0x02;
        newprotocol1[1] = 'F';
        newprotocol1[2] = 0x30;
        newprotocol1[3] = 0x30+cmd;//N楼
        int Sum = 0;
        for(i = 1;i<4;i++)
        Sum ^= newprotocol1[i];
        newprotocol1[4]=asc[Sum&0x0f];//校验F
        newprotocol1[5] = 0x03;

        qDebug() <<"send:"<< newprotocol1.toHex();

        m_serial->write( newprotocol1 );
        m_txbuffer.clear();
        m_txbuffer = newprotocol1.mid(0,6);
        //m_rxdata += newprotocol1.mid(0,6);
        txrepeat = true;
    }
    else if(cmd==9)
    {
            //开门协议
            QByteArray newprotocol1;
            newprotocol1.resize ( 5 );
            newprotocol1[0] = 0x02;
            newprotocol1[1] = 'O';
            newprotocol1[2] = '1';//开门
            int Sum = 0;
            for(i = 1;i<3;i++)
            Sum ^= newprotocol1[i];
            newprotocol1[3]=asc[Sum&0x0f];//校验F
            newprotocol1[4] = 0x03;

            qDebug() <<"send:"<< newprotocol1.toHex();

            m_serial->write( newprotocol1 );
            m_txbuffer.clear();
            m_txbuffer = newprotocol1.mid(0,5);
            txrepeat = true;
    }
    else if(cmd==10)
    {
            //关门协议
            QByteArray newprotocol1;
            newprotocol1.resize ( 5 );
            newprotocol1[0] = 0x02;
            newprotocol1[1] = 'L';
            newprotocol1[2] = '1';//关门
            int Sum = 0;
            for(i = 1;i<3;i++)
            Sum ^= newprotocol1[i];
            newprotocol1[3]=asc[Sum&0x0f];//校验F
            newprotocol1[4] = 0x03;

            qDebug()<<"send:" << newprotocol1.toHex();

            m_serial->write( newprotocol1 );
            m_txbuffer.clear();
            m_txbuffer = newprotocol1.mid(0,5);
            txrepeat = true;
    }
    else if(cmd==11)
    {
            //风扇协议
            QByteArray newprotocol1;
            newprotocol1.resize ( 5 );
            newprotocol1[0] = 0x02;
            newprotocol1[1] = 'B';
            newprotocol1[2] = '1';//风扇开
            int Sum = 0;
            for(i = 1;i<3;i++)
            Sum ^= newprotocol1[i];
            newprotocol1[3]=asc[Sum&0x0f];//校验F
            newprotocol1[4] = 0x03;

            qDebug() <<"send:"<< newprotocol1.toHex();

            m_serial->write( newprotocol1 );
            m_txbuffer.clear();
            m_txbuffer = newprotocol1.mid(0,5);
            txrepeat = true;
    }
    else if(cmd==12)
    {
            //风扇协议
            QByteArray newprotocol1;
            newprotocol1.resize ( 5 );
            newprotocol1[0] = 0x02;
            newprotocol1[1] = 'B';
            newprotocol1[2] = '0';//风扇关
            int Sum = 0;
            for(i = 1;i<3;i++)
            Sum ^= newprotocol1[i];
            newprotocol1[3]=asc[Sum&0x0f];//校验F
            newprotocol1[4] = 0x03;

            qDebug() <<"send:"<< newprotocol1.toHex();

            m_serial->write( newprotocol1 );
            m_txbuffer.clear();
            m_txbuffer = newprotocol1.mid(0,5);
            txrepeat = true;
    }
    else if(cmd==13)
    {
            //照明协议
            QByteArray newprotocol1;
            newprotocol1.resize ( 5 );
            newprotocol1[0] = 0x02;
            newprotocol1[1] = 'E';
            newprotocol1[2] = '1';//照明开
            int Sum = 0;
            for(i = 1;i<3;i++)
            Sum ^= newprotocol1[i];
            newprotocol1[3]=asc[Sum&0x0f];//校验F
            newprotocol1[4] = 0x03;

            qDebug()<<"send:" << newprotocol1.toHex();

            m_serial->write( newprotocol1 );
            m_txbuffer.clear();
            m_txbuffer = newprotocol1.mid(0,5);
            txrepeat = true;
    }
    else if(cmd==14)
    {
            //照明协议
            QByteArray newprotocol1;
            newprotocol1.resize ( 5 );
            newprotocol1[0] = 0x02;
            newprotocol1[1] = 'E';
            newprotocol1[2] = '0';//照明关
            int Sum = 0;
            for(i = 1;i<3;i++)
            Sum ^= newprotocol1[i];
            newprotocol1[3]=asc[Sum&0x0f];//校验F
            newprotocol1[4] = 0x03;

            qDebug()<<"send:" << newprotocol1.toHex();

            m_serial->write( newprotocol1 );
            m_txbuffer.clear();
            m_txbuffer = newprotocol1.mid(0,5);
            txrepeat = true;
    }
    else if(cmd==15)
    {
            //警铃协议
            QByteArray newprotocol1;
            newprotocol1.resize ( 5 );
            newprotocol1[0] = 0x02;
            newprotocol1[1] = 'J';
            newprotocol1[2] = '1';//警铃开
            int Sum = 0;
            for(i = 1;i<3;i++)
            Sum ^= newprotocol1[i];
            newprotocol1[3]=asc[Sum&0x0f];//校验F
            newprotocol1[4] = 0x03;

            qDebug() <<"send:"<< newprotocol1.toHex();

            m_serial->write( newprotocol1 );
            m_txbuffer.clear();
            m_txbuffer = newprotocol1.mid(0,5);
            txrepeat = true;
    }
    else if(cmd==16)
    {
            //警铃协议
            QByteArray newprotocol1;
            newprotocol1.resize ( 5 );
            newprotocol1[0] = 0x02;
            newprotocol1[1] = 'J';
            newprotocol1[2] = '0';//警铃关
            int Sum = 0;
            for(i = 1;i<3;i++)
            Sum ^= newprotocol1[i];
            newprotocol1[3]=asc[Sum&0x0f];//校验F
            newprotocol1[4] = 0x03;

            qDebug() <<"send:"<< newprotocol1.toHex();

            m_serial->write( newprotocol1 );
            m_txbuffer.clear();
            m_txbuffer = newprotocol1.mid(0,5);
            txrepeat = true;
    }
    else if(cmd==17)
    {
            //软件版本查询协议，
            QByteArray newprotocol1;
            newprotocol1.resize ( 4 );
            newprotocol1[0] = 0x02;
            newprotocol1[1] = 'W';
            int Sum = 0;
            for(i = 1;i<2;i++)
            Sum ^= newprotocol1[i];
            newprotocol1[2]=asc[Sum&0x0f];//校验F
            newprotocol1[3] = 0x03;

            qDebug() <<"send:"<< newprotocol1.toHex();

            m_serial->write( newprotocol1 );
            m_txbuffer.clear();
            m_txbuffer = newprotocol1.mid(0,4);
            txrepeat = true;
    }
    else if(cmd==18)
    {
            //（1） 轿厢型号协议: ‘1’ 主操纵厢,‘2’ 后操纵厢, ‘3’ 残疾人操纵厢,‘4’ 副操纵厢
            //（2） 选层器在刚初始化后立即发送，收到转换板回复后停止发送；
            //（3） 参数K01有改变后立即发送，收到转换板回复后停止发送；
            QByteArray newprotocol1;
            newprotocol1.resize ( 5 );
            newprotocol1[0] = 0x02;
            newprotocol1[1] = 'C';
            newprotocol1[2] = '1';//主操纵厢
            int Sum = 0;
            for(i = 1;i<3;i++)
            Sum ^= newprotocol1[i];
            newprotocol1[3]=asc[Sum&0x0f];//校验F
            newprotocol1[4] = 0x03;

            qDebug() <<"send:"<< newprotocol1.toHex();

            m_serial->write( newprotocol1 );
            m_txbuffer.clear();
            m_txbuffer = newprotocol1.mid(0,5);
            txrepeat = true;
    }
    else if(cmd==19)
    {
            //（1） 轿厢型号协议: ‘1’ 主操纵厢,‘2’ 后操纵厢, ‘3’ 残疾人操纵厢,‘4’ 副操纵厢
            //（2） 选层器在刚初始化后立即发送，收到转换板回复后停止发送；
            //（3） 参数K01有改变后立即发送，收到转换板回复后停止发送；
            QByteArray newprotocol1;
            newprotocol1.resize ( 5 );
            newprotocol1[0] = 0x02;
            newprotocol1[1] = 'C';
            newprotocol1[2] = '2';//后操纵厢
            int Sum = 0;
            for(i = 1;i<3;i++)
            Sum ^= newprotocol1[i];
            newprotocol1[3]=asc[Sum&0x0f];//校验F
            newprotocol1[4] = 0x03;

            qDebug() <<"send:"<< newprotocol1.toHex();

            m_serial->write( newprotocol1 );
            m_txbuffer.clear();
            m_txbuffer = newprotocol1.mid(0,5);
            txrepeat = true;
    }
    else if(cmd==20)
    {
            //（1） 轿厢型号协议: ‘1’ 主操纵厢,‘2’ 后操纵厢, ‘3’ 残疾人操纵厢,‘4’ 副操纵厢
            //（2） 选层器在刚初始化后立即发送，收到转换板回复后停止发送；
            //（3） 参数K01有改变后立即发送，收到转换板回复后停止发送；
            QByteArray newprotocol1;
            newprotocol1.resize ( 5 );
            newprotocol1[0] = 0x02;
            newprotocol1[1] = 'C';
            newprotocol1[2] = '3';//残疾人操纵厢
            int Sum = 0;
            for(i = 1;i<3;i++)
            Sum ^= newprotocol1[i];
            newprotocol1[3]=asc[Sum&0x0f];//校验F
            newprotocol1[4] = 0x03;

            qDebug() <<"send:"<< newprotocol1.toHex();

            m_serial->write( newprotocol1 );
            m_txbuffer.clear();
            m_txbuffer = newprotocol1.mid(0,5);
            txrepeat = true;
    }
    else if(cmd==21)
    {
            //（1） 轿厢型号协议: ‘1’ 主操纵厢,‘2’ 后操纵厢, ‘3’ 残疾人操纵厢,‘4’ 副操纵厢
            //（2） 选层器在刚初始化后立即发送，收到转换板回复后停止发送；
            //（3） 参数K01有改变后立即发送，收到转换板回复后停止发送；
            QByteArray newprotocol1;
            newprotocol1.resize ( 5 );
            newprotocol1[0] = 0x02;
            newprotocol1[1] = 'C';
            newprotocol1[2] = '4';//副操纵厢
            int Sum = 0;
            for(i = 1;i<3;i++)
            Sum ^= newprotocol1[i];
            newprotocol1[3]=asc[Sum&0x0f];//校验F
            newprotocol1[4] = 0x03;

            qDebug() <<"send:"<< newprotocol1.toHex();

            m_serial->write( newprotocol1 );
            m_txbuffer.clear();
            m_txbuffer = newprotocol1.mid(0,5);
            txrepeat = true;
    }
    else if(cmd==23)
    {
            //背光
            QByteArray newprotocol1;
            newprotocol1.resize ( 5 );
            newprotocol1[0] = 0x02;
            newprotocol1[1] = 'D';
            newprotocol1[2] = '1';//背光开
            int Sum = 0;
            for(i = 1;i<3;i++)
            Sum ^= newprotocol1[i];
            newprotocol1[3]=asc[Sum&0x0f];//校验F
            newprotocol1[4] = 0x03;

            //qDebug() << newprotocol1.toHex();

            m_serial->write( newprotocol1 );
            m_txbuffer.clear();
            m_txbuffer = newprotocol1.mid(0,5);
            txrepeat = true;
    }
    else if(cmd==22)
    {
            QByteArray newprotocol1;
            newprotocol1.resize ( 5 );
            newprotocol1[0] = 0x02;
            newprotocol1[1] = 'D';
            newprotocol1[2] = '0';//背光关
            int Sum = 0;
            for(i = 1;i<3;i++)
            Sum ^= newprotocol1[i];
            newprotocol1[3]=asc[Sum&0x0f];//校验F
            newprotocol1[4] = 0x03;

            //qDebug() << newprotocol1.toHex();

            m_serial->write( newprotocol1 );
            m_txbuffer.clear();
            m_txbuffer = newprotocol1.mid(0,5);
            txrepeat = true;
    }

//    if(!m_txbuffer.isEmpty())
//    {
//        m_serial->write(m_txbuffer);
//        txrepeat = false;
//    }
}

void  Serialportpro::test()
{
    char asc[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D','E', 'F'};
    int i=1;
    QByteArray newprotocol1;
    newprotocol1.resize ( 11+16+3 );
    newprotocol1[0] = 0x02;
    newprotocol1[1] = 'S';
    newprotocol1[2] = 0x30;
    newprotocol1[3] = 0x30+test_flr;//N楼
    if(test_dir==1)
        newprotocol1[4] = 0x31;
    else
        newprotocol1[4] = 0x32;

    newprotocol1[5] = 0x30+test_flr%8;
    newprotocol1[6] = 0x30;
    newprotocol1[7] = 0x30;
    newprotocol1[8] = 0x30;

    for(i=0;i<16;i++)
    {
       newprotocol1[9+i] = 'F';
    }

    int Sum = 0;
    for(i = 1;i<9+16+3;i++)
    Sum ^= newprotocol1[i];
    newprotocol1[9+16+3]=asc[Sum&0x0f];//校验F
    newprotocol1[10+16+3] = 0x03;

    //qDebug() << newprotocol1.toHex();

    m_rxdata += newprotocol1.mid(0,11+16+3);

    newprotocol1.resize ( 20 );
    newprotocol1[0] = 0x02;
    newprotocol1[1] = 'W';
    newprotocol1[2] = 'V';
    newprotocol1[3] = 0x31;
    newprotocol1[4] = '.';
    newprotocol1[5] = 0x30;
    newprotocol1[6] = 0x30;
    newprotocol1[7] = 0x30;
    newprotocol1[8] = 0x30;
    newprotocol1[9] = 0x30;
    newprotocol1[10] = 0x30;
    newprotocol1[11] =0x30;
    newprotocol1[12] = 0x30+test_flr;
    newprotocol1[13] = 0x30;
    newprotocol1[14] = 0x30;
    newprotocol1[15] = 0x30;
    newprotocol1[16] = 0x30;
    newprotocol1[17] = 0x30;

    Sum = 0;
    for(i = 1;i<18;i++)
    Sum ^= newprotocol1[i];
    newprotocol1[i]=asc[Sum&0x0f];//校验F
    newprotocol1[19] = 0x03;

     m_rxdata += newprotocol1.mid(0,20);

    test_flr += test_dir;
     if(test_flr>8)
     {
            test_dir = -1;
            test_flr = 8;
     }
     else if(test_flr<1)
     {
         test_dir = 1;
         test_flr = 1;
     }

}
